package com.linxz.framework.ui.loader;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.linxz.framework.R;
/**
 * @author Linxz
 * 创建日期：2019年02月20日 20:32
 * version：1.0.0
 * 描述：
 */
public class DefaultLoadingProgress extends Dialog {
    public static DefaultLoadingProgress dialog;

    public DefaultLoadingProgress(Context context) {
        super(context);
    }

    public DefaultLoadingProgress(Context context, int theme) {
        super(context, theme);
    }

    /**
     * 当窗口焦点改变时调用
     */
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        ImageView imageView = (ImageView) findViewById(R.id.spinnerImageView);
        // 获取ImageView上的动画背景
        AnimationDrawable spinner = (AnimationDrawable) imageView.getBackground();
        // 开始动画
        spinner.start();
    }

    /**
     * 给Dialog设置提示信息
     *
     * @param message
     */
    public void setMessage(CharSequence message) {
        if (message != null && message.length() > 0) {
            findViewById(R.id.message).setVisibility(View.VISIBLE);
            TextView txt =  findViewById(R.id.message);
            txt.setText(message);
        }else{
            findViewById(R.id.message).setVisibility(View.GONE);
        }
    }

    /**
     * 弹出自定义ProgressDialog
     *
     * @param context
     *            上下文
     * @param message
     *            提示
     * @param cancelable
     *            是否按返回键取消
     * @param cancelListener
     *            按下返回键监听
     * @return
     */
    public static DefaultLoadingProgress show(Context context, CharSequence message,
                                              boolean cancelable, boolean cancelableBack,
                                              OnCancelListener cancelListener) {
        dialog = new DefaultLoadingProgress(context, R.style.Loading_Progress);
        dialog.setTitle("");
        dialog.setContentView(R.layout.framework_loading_progress);
        dialog.setMessage(message);
        // 按任何键是否取消
        /**
         * true 为确定 可以点击可消失 false点击不消失
         */
        dialog.setCancelable(cancelable);
        // 点击返回键才能取消
        dialog.setCanceledOnTouchOutside(cancelableBack);
        // 监听返回键处理
        dialog.setOnCancelListener(cancelListener);
        // 设置居中
        dialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        // 设置背景层透明度
        lp.dimAmount = 0.2f;
        dialog.getWindow().setAttributes(lp);
        // dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
        dialog.show();
        return dialog;
    }

    public static void dismissDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog=null;
        }

    }

    public static boolean isShown(){
        if(dialog!=null&&dialog.isShowing()){
            return true;
        }else{
            return  false;
        }
    }

    public static DefaultLoadingProgress newInstance(Context context, CharSequence message, boolean cancelable, boolean cancelableBack,
                                                     OnCancelListener cancelListener){
        DefaultLoadingProgress dialog = new DefaultLoadingProgress(context, R.style.Loading_Progress);
        dialog.setTitle("");
        dialog.setContentView(R.layout.framework_loading_progress);
        dialog.setMessage(message);
        // 按任何键是否取消
        /**
         * true 为确定 可以点击可消失 false点击不消失
         */
        dialog.setCancelable(cancelable);
        // 点击返回键才能取消
        dialog.setCanceledOnTouchOutside(cancelableBack);
        // 监听返回键处理
        dialog.setOnCancelListener(cancelListener);
        // 设置居中
        dialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        // 设置背景层透明度
        lp.dimAmount = 0.2f;
        dialog.getWindow().setAttributes(lp);
        return dialog;
    }
}